﻿// owner 蒋家伟

#include "SocketHardwareDevice.h"

#include "SocketHardwareCompoent.h"
#include "ISocketHardwareExternalData.h"
#include "Dbg_Warn_Define.h"

using namespace jsdk;

OwnerPtr<ISocketHardwareDevice> jsdk::SocketHardwareDevice::create(OwnerPtr<ISocketHardwareExternalData> opSocketHardwareExternalData)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareExternalData, L"外部数据为空", L"蒋家伟", L"2023-08-03");
    OwnerPtr<SocketHardwareDevice> opSocketHardwareDevice = NEW_AS_OWNERPTR(SocketHardwareDevice);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareDevice, L"套接字硬件设备创建失败", L"蒋家伟", L"2023-08-03");
    opSocketHardwareDevice->m_opSocketHardwareCompoent.swap(SocketHardwareCompoent::create(opSocketHardwareExternalData->getHardwareEventHandler(), opSocketHardwareExternalData->getSocketEventHandler()));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareDevice->m_opSocketHardwareCompoent, L"套接字硬件组件创建失败", L"蒋家伟", L"2023-08-03");
    opSocketHardwareDevice->m_opExternalData.swap(opSocketHardwareExternalData);

    if (opSocketHardwareDevice->m_opExternalData)
    {
        opSocketHardwareDevice->m_opExternalData->setOwnerDevice(opSocketHardwareDevice.get());
    }

    return std::move(opSocketHardwareDevice);
}

bool jsdk::SocketHardwareDevice::start()
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opSocketHardwareCompoent, L"套接字硬件组件初始化失败", L"蒋家伟", L"2023-08-03");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opExternalData, L"外部参数无效", L"蒋家伟", L"2023-08-03");
    std::string ipAddress;
    int64_t port;
    bool success = m_opExternalData->getSocketParameter(ipAddress, port);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"从外部数据中获取ip地址与端口失败", L"蒋家伟", L"2023-08-03");
    return m_opSocketHardwareCompoent->start(ipAddress, port);
}

void jsdk::SocketHardwareDevice::stop()
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opSocketHardwareCompoent, L"套接字硬件组件初始化失败", L"蒋家伟", L"2023-08-03");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opExternalData, L"外部参数无效", L"蒋家伟", L"2023-08-03");
    m_opSocketHardwareCompoent->stop();
    m_opExternalData->stop();
}

void jsdk::SocketHardwareDevice::addTask(const TaskId & taskId) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opSocketHardwareCompoent, L"套接字硬件组件初始化失败", L"蒋家伟", L"2023-08-03");
    m_opSocketHardwareCompoent->addTask(taskId);
}

const std::wstring jsdk::SocketHardwareDevice::getDeviceName() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_opExternalData, L"",L"套接字硬件组件初始化失败", L"蒋家伟", L"2023-08-03");
    return m_opExternalData->getHardwareName();
}

const IDeviceExternalData* jsdk::SocketHardwareDevice::getExternalData() const
{
    return m_opExternalData.get();
}

IDeviceExternalData* jsdk::SocketHardwareDevice::getExternalDataFW()
{
    return m_opExternalData.get();
}

void jsdk::SocketHardwareDevice::sendMessage(std::string && msg, int64_t length) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opSocketHardwareCompoent, L"套接字硬件组件初始化失败", L"蒋家伟", L"2023-08-03");
    m_opSocketHardwareCompoent->sendMessage(std::move(msg), length);
}

void jsdk::SocketHardwareDevice::reConnect() const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opSocketHardwareCompoent, L"套接字硬件组件初始化失败", L"蒋家伟", L"2023-08-03");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opExternalData, L"外部参数无效", L"蒋家伟", L"2023-08-03");
    std::string ipAddress;
    int64_t port;
    bool success = m_opExternalData->getSocketParameter(ipAddress, port);
    DBG_WARN_AND_RETURN_VOID_UNLESS(success, L"从外部数据中获取ip地址与端口失败", L"蒋家伟", L"2023-08-03");
    m_opSocketHardwareCompoent->reConnect(ipAddress, port);
}

bool jsdk::SocketHardwareDevice::isOnline() const
{
    if (m_opSocketHardwareCompoent)
    {
        return m_opSocketHardwareCompoent->isOnline();
    }
    return false;
}

